<cfcomponent output="false">
	
	<!--- --->
	
	<cffunction name="getWorksheet" access="public" output="true" returntype="query">
		<cfargument name="worksheetID" required="false" default="" />
		
		<cfset var local = {} />

		<cfquery name="local.getWorksheet" datasource="#$.site.getVar('datasource')#">
			select 
			hex(worksheet.id) as id,
			worksheet.name,
			worksheet.code,
			worksheet.sort_order,
			worksheet.description,
			worksheet.field_count
			from worksheet
			where worksheet.active = 1
			<cfif arguments.worksheetID neq ''>
				and worksheet.id = #$.database.uuid(arguments.worksheetID)#
			</cfif>
			order by sort_order asc
		</cfquery>
		
		<cfreturn local.getWorksheet />
		
	</cffunction>
	
	<!--- --->
	
	<cffunction name="getWorksheetFields" access="public" output="true" returntype="query">
		<cfargument name="worksheetID" required="false" default="" />
		
		<cfset var local = {} />

		<cfquery name="local.getWorksheetFields" datasource="#$.site.getVar('datasource')#">
			select 
			hex(worksheet_field.id) as id,
			worksheet_field.name,
			worksheet_field.description,
			worksheet_field.field_type,
			worksheet_field.field_number,
			worksheet_field_user.value as user_value,
			hex(worksheet_field_user.id) as worksheet_field_user_id,
			worksheet_field.default_value
			from worksheet_field
			left join worksheet_field_user on worksheet_field_user.worksheet_field_id = worksheet_field.id
				and worksheet_field_user.user_id = #$.database.uuid(request.session.user.id)#
			where worksheet_field.active = 1
			<cfif arguments.worksheetID neq ''>
				and worksheet_field.worksheet_id = #$.database.uuid(arguments.worksheetID)#
			</cfif>
			order by worksheet_field.field_number asc
		</cfquery>
		
		<cfreturn local.getWorksheetFields />
		
	</cffunction>
	
	<!--- --->

</cfcomponent>